
#include "conf.h"
#include "API.h"
#include "Core/OpCode.h"
#include "Core/DosExe.h"
#include "Core/DosRuntime.h"
#include "Core/DosExecuteCore.h"

DosExe*			Dos = NULL;
DosRuntime*		DosRT = NULL;
DosExecuteCore* DosEC = NULL;

bool isInitOpCode = false;

API const char* Version()
{
	return VERSION;
}
API int Init(){
	if (isInitOpCode==true) return 0; 

    DosRT	= new DosRuntime();
    DosEC	= new DosExecuteCore();

	DosEC->Load(DosRT);

	InitOpCode(); 
	isInitOpCode = true;

	return 0;
}
API int BindIntCall(void* func)
{
	INTCall = (IntCall*)func;
	return 0;
}

API char* ReadIOBuffer()
{
	return (char*)IOBuffer::Read();
}
API int WriteIOBuffer(char* buff, int size)
{
	IOBuffer::Write((BYTE*)buff, size);
	return 0;
}
API int ClearIOBuffer()
{
	IOBuffer::Clear();
	return 0;
}

API unsigned char* GetExeByte(){
	return (unsigned char*)Dos->GetDosExeByte();
}
API int GetExeLengh(){
	return Dos->GetExeLengh();
}
API unsigned char* GetMemory(){
	return (unsigned char*)DosRT->GetMemory();
}
API int GetMemorySize(){
	return MEMORY_SIZE;
}
API void* GetRegister(){
	return (void*)DosRT->GetRegister();
}

API int LoadExe(const char* ExeName){

	if (Dos != NULL) delete Dos;

	Dos = new DosExe();
	Dos->Load(ExeName);
	DosRT->Load(Dos);

	return 0;
}
API int SetMemory(void* mem){
	return DosRT->SetMemory((BYTE*)mem);
}
API int LoadToMem(int Base){
	DosRT->LoadToMemory(Base);
	DosRT->InitRegister();
	return 0;
}
API int Relocation(){
	DosRT->Relocation();
	return 0;
}

API int RunStep(){
	OpCodeReturn re;
	DosEC->RunStep(&re);
	return re.integer;
}
API int Disassem(char* buff){
	OpCodeReturn re;
	DosEC->Disassem(&re);
	strcpy(buff, re.string.c_str());
	return re.integer;
}
API int DisassemByIP(char* buff, WORD ip){
	OpCodeReturn re;
	DosEC->Disassem(&re, ip);
	strcpy(buff, re.string.c_str());
	return re.integer;
}
